﻿using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for OrderDetailStatusEditorView.xaml
    /// </summary>
    public partial class OrderDetailStatusEditorView
    {
        private readonly OrderDetailStatusController _orderDetailStatusControl;
        private readonly OrderDetailStatus _orderDetailStatus;

        public OrderDetailStatusEditorView()
        {
            InitializeComponent();

            _orderDetailStatusControl = new OrderDetailStatusController();
            Title = "Add Order Detail Status";
        }

        public OrderDetailStatusEditorView(OrderDetailStatus orderDetailStatus)
        {
            InitializeComponent();
            _orderDetailStatusControl = new OrderDetailStatusController();
            Title = "Edit Order Detail Status";

            _orderDetailStatus = orderDetailStatus;

            textBoxId.Text = orderDetailStatus.Id.ToString();
            textBoxStatus.Text = orderDetailStatus.Status;
        }

        private void ClearTextBox()
        {
            textBoxId.Text = "";
            textBoxStatus.Text = "";
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (textBoxStatus.Text.Equals(""))
            {
                MessageBox.Show("field tidak boleh kosong");
            }
            else
            {
                if (Title.Equals("Add Order Detail Status"))
                {
                    var orderDetailStatus = new OrderDetailStatus
                                     {
                                         Status = textBoxStatus.Text
                                     };

                    var message = _orderDetailStatusControl.AddOrderDetailStatus(orderDetailStatus);

                    switch (message)
                    {
                        case 1:
                            MessageBox.Show("sukses menambah order detail status");
                            ClearTextBox();
                            break;
                        default:
                            MessageBox.Show("status sudah ada");
                            break;
                    }
                }
                else
                {
                    var newOrderDetailStatus = new OrderDetailStatus
                                        {
                                            Id = _orderDetailStatus.Id,
                                            Status = textBoxStatus.Text
                                        };

                    var message = _orderDetailStatusControl.EditOrderDetailStatus(newOrderDetailStatus, _orderDetailStatus.Status);

                    switch (message)
                    {
                        case 1:
                            MessageBox.Show("sukses mengedit order detail status");
                            break;
                        default:
                            MessageBox.Show("status sudah ada");
                            break;
                    }
                }
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
